﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console;
using Mimoza.Apps.Console.ViewModel;

namespace Console
{
    public partial class MainForm : Form
    {
        private List<ToolStripItem> m_toolStripItemList = new List<ToolStripItem>();
        Mimoza.Apps.Console.Application m_application = null;
        Mimoza.Apps.Console.View.ToolBarView m_toolBarView = null;
        Mimoza.Apps.Console.View.HostsView m_hostsView = null;
        Mimoza.Apps.Console.View.PolicyView m_policyView = null;
        Mimoza.Apps.Console.View.UpdateView m_updateView = null;
        Mimoza.Apps.Console.View.ContainerTreeView m_containerTreeView = null;

        Mimoza.Apps.Console.View.QueryByTimeView m_queryByTimeView;
        Mimoza.Apps.Console.View.QueryByPlatformView m_queryByPlatform;

        Dictionary<BaseViewModel, TabPage> m_viewModelsPages = new Dictionary<BaseViewModel,TabPage>();
        

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            {
                Mimoza.Apps.Console.Configuration configuration = new Mimoza.Apps.Console.Configuration();

                m_application = new Mimoza.Apps.Console.Application(configuration);
                m_toolBarView = new Mimoza.Apps.Console.View.ToolBarView();
                m_hostsView = new Mimoza.Apps.Console.View.HostsView();
                m_policyView = new Mimoza.Apps.Console.View.PolicyView();
                m_updateView = new Mimoza.Apps.Console.View.UpdateView();
                m_containerTreeView = new Mimoza.Apps.Console.View.ContainerTreeView();

                m_application.Init(this);
                m_toolBarView.Init(m_application.ToolBarViewModel);
                m_hostsView.Init(m_application.HostsViewModel);
                m_policyView.Init(m_application.PolicyViewModel);
                m_updateView.Init(m_application.UpdateViewModel);
                m_containerTreeView.Init(m_application.ContainersTreeViewModel);

                toolBarPanel.Controls.Add(m_toolBarView);
                treeViewPanel.Controls.Add(m_containerTreeView);

                InitQueriesView();

                AddPageLink(m_hostsView, tabPageHosts);
                AddPageLink(m_policyView, tabPagePolicies);
                AddPageLink(m_updateView, tabPageUpdate);

                m_application.ContainersTreeViewModel.Visible = true;
            }
         }

        void InitQueriesView()
        {
            m_queryByTimeView = new Mimoza.Apps.Console.View.QueryByTimeView();
            m_queryByTimeView.Init(m_application.QueryByTimeViewModel);

            AddPageLink(m_queryByTimeView, tabPageQuery);

            m_queryByPlatform = new Mimoza.Apps.Console.View.QueryByPlatformView();
            m_queryByPlatform.Init(m_application.QueryByPlatformViewModel);

            AddPageLink(m_queryByPlatform, tabPageQueryPlatfrom);
        }

        private void AddPageLink(Mimoza.Apps.Console.View.BaseView view, System.Windows.Forms.TabPage tabPage)
        {
            tabPage.Controls.Add(view);
            this.rootTab.TabPages.Remove(tabPage);

            view.ViewModel.Visible = false;

            m_viewModelsPages[view.ViewModel] = tabPage;
            view.ViewModel.PropertyChanged += new PropertyChangedEventHandler(ViewModel_PropertyChanged);
        }

        void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Visible")
            {
                BaseViewModel viewModel = sender as BaseViewModel;
                if (m_viewModelsPages.ContainsKey(viewModel))
                {
                    TabPage tabPage = m_viewModelsPages[viewModel];

                    if (viewModel.Visible && !this.rootTab.TabPages.Contains(tabPage))
                    {
                        this.rootTab.TabPages.Add(tabPage);

                        Mimoza.Apps.Console.View.BaseView view = tabPage.Controls[0] as
                            Mimoza.Apps.Console.View.BaseView;
                        if (view != null) view.OnActivate();
                    }
                    else if (!viewModel.Visible && this.rootTab.TabPages.Contains(tabPage))
                    {
                        this.rootTab.TabPages.Remove(tabPage);
                    }
                }
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_application.Stop();
        }

        private void rootTab_Selected(object sender, TabControlEventArgs e)
        {
            switch ((sender as TabControl).Name)
            {
                case "tabPageHosts":
                    m_application.HostsViewModel.InFocus = true;
                    m_application.PolicyViewModel.InFocus = false;
                    m_application.UpdateViewModel.InFocus = false;
                    break;
                case "tabPagePolicies":
                    m_application.HostsViewModel.InFocus = false;
                    m_application.PolicyViewModel.InFocus = true;
                    m_application.UpdateViewModel.InFocus = false;
                    break;
                case "tabPageUpdate":
                    m_application.HostsViewModel.InFocus = false;
                    m_application.PolicyViewModel.InFocus = false;
                    m_application.UpdateViewModel.InFocus = true;
                    break;
            }
        }

        private void timerCheckConnection_Tick(object sender, EventArgs e)
        {
            m_application.Configuration.CheckServerConnection();
        }
    }
}
